﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using YJYpublicClass;
using HalconDotNet;

namespace WindowsFormsApp1.Forms
{
    public partial class 视觉处理 : Form
    {
        public 视觉处理()
        {
            InitializeComponent();
        }

        private void btn_图像处理加载_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "ALL Image Files|*.*";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = openFileDialog1.FileName;

                if (PublicFuntions.IsopenHdp(path))
                {
                    this.txt_工位1图像处理路径.Text = openFileDialog1.FileName;

                    PublicValue.mainHProgramPath[0] = this.txt_工位1图像处理路径.Text;
                    //可以更新一下图像处理

                    PublicFuntions.WriteINI("图像处理", "主函数路径", PublicValue.mainHProgramPath[0], PublicValue.StationINIpath[0]);
                    MessageBox.Show("修改路径需重新加载程序");
                }
                else
                {
                    MessageBox.Show("不是一个halcon函数，路径不正确");
                }

            }
        }

        private void btn_设置_Click(object sender, EventArgs e)
        {
            double exTime=0.0;
            try
            {
                double.TryParse(this.txt_曝光.Text.Trim(),out exTime);

            }
            catch (Exception ex)
            {

                return;
            }
            try
            {
                
                if (外设.Camera.openflag)
                {
                    外设.Camera.setexposure(exTime);
                    //保存数据PublicValue.DexposureTime[i] = Convert.ToDouble( PublicFuntions.GetINI("图像处理", "曝光时间", "", PublicValue.StationINIpath[i]));
                    PublicFuntions.WriteINI("相机数据", "曝光时间", exTime.ToString(), PublicValue.StationINIpath[0]);
                    PublicValue.DexposureTime[0] = exTime;
                }
                else
                {
                    MessageBox.Show("相机未连接");
                }
               
                       
                


            }
            catch (Exception ex)
            {

                MessageBox.Show("设置失败"+ex.Message);
            }
        }

        private void btn_连续采集_Click(object sender, EventArgs e)
        {

        }

        private void 视觉处理_Load(object sender, EventArgs e)
        {
            if(外设.Camera.openflag)
            {
                this.btn_设置曝光.Enabled = true;
                
            }

            //在这里再cmb窗口中设置相机
            for (int i = 0; i < HKCamera.deviceName.Count;i++ )
            {
                cmb_工位1相机选择.Items.Add(HKCamera.deviceName[i]);
                cmb_工位2相机选择.Items.Add(HKCamera.deviceName[i]);

            }

            cmb_工位1相机选择.Text = PublicValue.CameraSerialNum[0];
            cmb_工位2相机选择.Text = PublicValue.CameraSerialNum[1];


            this.txt_曝光.Text = PublicValue.DexposureTime[0].ToString();
            this.txt_工位1图像处理路径.Text = PublicValue.mainHProgramPath[0];
            this.txt_工位2图像处理路径.Text = PublicValue.mainHProgramPath[1];
            this.txt_工位3图像处理路径.Text = PublicValue.mainHProgramPath[2];
            this.txt_工位4图像处理路径.Text = PublicValue.mainHProgramPath[3];
            this.txt_工位5图像处理路径.Text = PublicValue.mainHProgramPath[4];
            this.txt_工位6图像处理路径.Text = PublicValue.mainHProgramPath[5];

            //外设.Camera.m_Window = this.hWindowControl1.HalconWindow;
            //外设.Camera2.m_Window = this.hWindowControl1.HalconWindow;
            //外设.Camera3.m_Window = this.hWindowControl1.HalconWindow;

            //属性参数
            //ppG_工位1参数.SelectedObject = 外设.S1stationPrasR;




        }

        private void btn_工位2图像处理加载_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "ALL Image Files|*.*";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = openFileDialog1.FileName;

                if (PublicFuntions.IsopenHdp(path))
                {
                    this.txt_工位2图像处理路径.Text = openFileDialog1.FileName;

                    PublicValue.mainHProgramPath[1] = this.txt_工位2图像处理路径.Text;
                    //可以更新一下图像处理

                    PublicFuntions.WriteINI("图像处理", "主函数路径", PublicValue.mainHProgramPath[1], PublicValue.StationINIpath[1]);
                    MessageBox.Show("修改路径需重新加载程序");
                }
                else
                {
                    MessageBox.Show("不是一个halcon函数，路径不正确");
                }

            }
        }

        private void btn_工位3图像处理加载_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "ALL Image Files|*.*";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = openFileDialog1.FileName;

                if (PublicFuntions.IsopenHdp(path))
                {
                    this.txt_工位3图像处理路径.Text = openFileDialog1.FileName;

                    PublicValue.mainHProgramPath[2] = this.txt_工位3图像处理路径.Text;
                    //可以更新一下图像处理

                    PublicFuntions.WriteINI("图像处理", "主函数路径", PublicValue.mainHProgramPath[2], PublicValue.StationINIpath[2]);
                    MessageBox.Show("修改路径需重新加载程序");
                }
                else
                {
                    MessageBox.Show("不是一个halcon函数，路径不正确");
                }

            }
        }
        private void btn_工位4图像处理加载_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "ALL Image Files|*.*";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = openFileDialog1.FileName;

                if (PublicFuntions.IsopenHdp(path))
                {
                    this.txt_工位4图像处理路径.Text = openFileDialog1.FileName;

                    PublicValue.mainHProgramPath[3] = this.txt_工位4图像处理路径.Text;
                    //可以更新一下图像处理

                    PublicFuntions.WriteINI("图像处理", "主函数路径", PublicValue.mainHProgramPath[3], PublicValue.StationINIpath[3]);
                    MessageBox.Show("修改路径需重新加载程序");
                }
                else
                {
                    MessageBox.Show("不是一个halcon函数，路径不正确");
                }

            }
        }
        private void btn_工位5图像处理加载_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "ALL Image Files|*.*";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = openFileDialog1.FileName;

                if (PublicFuntions.IsopenHdp(path))
                {
                    this.txt_工位5图像处理路径.Text = openFileDialog1.FileName;

                    PublicValue.mainHProgramPath[4] = this.txt_工位5图像处理路径.Text;
                    //可以更新一下图像处理

                    PublicFuntions.WriteINI("图像处理", "主函数路径", PublicValue.mainHProgramPath[4], PublicValue.StationINIpath[4]);
                    MessageBox.Show("修改路径需重新加载程序");
                }
                else
                {
                    MessageBox.Show("不是一个halcon函数，路径不正确");
                }

            }
        }




       

        private void btn_工位1参数保存_Click(object sender, EventArgs e)
        {
            if (XmlHelper.SaveXML(PublicValue.S1prassavepath, 外设.S1stationPrasR) == "")
            {
                MessageBox.Show("保存成功");
            }
            else
            {
                MessageBox.Show("保存失败");
            }
        }

      

        private void btnSoftTrig_Click(object sender, EventArgs e)
        {
            try
            {
                外设.Camera.IsUpdateView = false;

                外设.Camera.softTriggerExcute();
                Thread.Sleep(1000);
                PublicFuntions.DispImage(外设.Camera.Image.Clone(), this.hWindowControl2.HalconWindow);
            }
            catch (Exception ex)
            {

                MessageBox.Show (ex .ToString());
            }
            
        }

        private void btn_工位6图像处理加载_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "ALL Image Files|*.*";
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = openFileDialog1.FileName;

                if (PublicFuntions.IsopenHdp(path))
                {
                    this.txt_工位6图像处理路径.Text = openFileDialog1.FileName;

                    PublicValue.mainHProgramPath[5] = this.txt_工位6图像处理路径.Text;
                    //可以更新一下图像处理

                    PublicFuntions.WriteINI("图像处理", "主函数路径", PublicValue.mainHProgramPath[5], PublicValue.StationINIpath[5]);
                    MessageBox.Show("修改路径需重新加载程序");
                }
                else
                {
                    MessageBox.Show("不是一个halcon函数，路径不正确");
                }
            }
        }
    }
}
